//
//  HWFriendHomeViewModel.m
//  HomeWork
//
//  Created by apple on 2017/10/26.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HWFriendHomeViewModel.h"
#import "HWUserInfo.h"
@interface HWFriendHomeViewModel()

/** 当前索引 */
@property (assign, nonatomic) NSInteger currentIndex;

@end

@implementation HWFriendHomeViewModel

- (void)initialize {
    
    @weakify(self);
    
    
    [[[self.refreshDataCommand.executing skip:1] take:1] subscribeNext:^(id x) {
        if ([x isEqualToNumber:@(YES)]) {
            [MBProgressHUD showMessage:@"" toView:Window];
        }
    }];
    
    [self.refreshDataCommand.executionSignals.switchToLatest subscribeNext:^(NSArray *arr) {
        
        @strongify(self);
        [MBProgressHUD hideHUDForView:Window];
        [self.refreshEndSubject sendNext:@(refreshEndStatusNormal)];
       
    }];
    
    [self.nextPageCommand.executionSignals.switchToLatest subscribeNext:^(NSArray *arr) {
        
        @strongify(self);
        if (arr.count < PageSize) {
            [self.refreshEndSubject sendNext:@(refreshEndStatusNoMoreData)];
        } else {
            [self.refreshEndSubject sendNext:@(refreshEndStatusNormal)];
        }
    }];
    
    [self.userInfoCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        @strongify(self);
        [self.userSubject sendNext:nil];
    }];
    
//    self.commentSubject = self.cellViewModel.commentSubject;
    [self cellViewModel];
    
}

#pragma makr - tableView相关
- (RACSubject *)refreshEndSubject {
    
    if (!_refreshEndSubject) {
        
        _refreshEndSubject = [RACSubject subject];
    }
    
    return _refreshEndSubject;
}

- (RACCommand *)refreshDataCommand {
    
    if (!_refreshDataCommand) {
        
        
        @weakify(self);
        _refreshDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            
            @strongify(self);
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                
                
                @strongify(self);
                self.currentIndex = 0;
                
                [HWRequestTool GET:@"http://thoughtworks-ios.herokuapp.com/user/jsmith/tweets" parameters:nil success:^(HWRequestTool *request, id response) {
                    [HWTweetDBTool deleteAllTweets];
                    
                    NSArray *tweets = (NSArray *)response;
                    if (!IsValidateArr(tweets)) {
                        return ;
                    }
                    [self.dataArray removeAllObjects];
                    for (NSDictionary *dic in tweets) { // 排除错误数据
                        if (IsValidateDic(dic)) {
                            NSString *content = dic[@"content"];
                            NSArray *images = dic[@"images"];
                            if (IsValidateString(content) || IsValidateArr(images)) {
                                HWTweetsModel *model = [HWTweetsModel mj_objectWithKeyValues:dic];
                                [HWTweetDBTool saveTweet:model];
                            }
                        }
                    }
                    
                    NSArray *showArr = [HWTweetDBTool getTweetWithIndex:self.currentIndex pageSize:PageSize];
                    [self.dataArray addObjectsFromArray:showArr];
                    self.currentIndex++;
                    [subscriber sendNext:self.dataArray];
                    [subscriber sendCompleted];
                } failure:^(HWRequestTool *request, NSError *error) {
                    [subscriber sendNext:self.dataArray];
                    [subscriber sendCompleted];
                }];
                
                return nil;
            }];
        }];
    }
    
    return _refreshDataCommand;
}

- (RACCommand *)nextPageCommand {
    
    if (!_nextPageCommand) {
        
        @weakify(self);
        _nextPageCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            
            @strongify(self);
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                @strongify(self);
                
                NSArray *addShow = [HWTweetDBTool getTweetWithIndex:self.currentIndex pageSize:PageSize];
                
                if (addShow.count < PageSize) {
                    [self.dataArray addObjectsFromArray:addShow];
                    
                } else {
                    [self.dataArray addObjectsFromArray:addShow];
                }
                [subscriber sendNext:addShow];
                [subscriber sendCompleted];
                self.currentIndex++;

                
                return nil;
            }];
        }];
    }
    
    return _nextPageCommand;
}
#pragma mark - 用户信息相关
- (RACCommand *)userInfoCommand{
    if (!_userInfoCommand) {
        @weakify(self);
        _userInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                @strongify(self);
                [HWRequestTool GET:@"http://thoughtworks-ios.herokuapp.com/user/jsmith" parameters:nil success:^(HWRequestTool *request, id response) {
                    HWUserInfo *userInfo = [HWUserInfo mj_objectWithKeyValues:response];
                    self.userInfo = userInfo;
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                } failure:^(HWRequestTool *request, NSError *error) {
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _userInfoCommand;
}

- (RACSubject *)userSubject{
    if (!_userSubject) {
        _userSubject = [RACSubject subject];
    }
    return _userSubject;
}


#pragma mark - 其他
- (HWTweetCellViewModel *)cellViewModel{
    if (!_cellViewModel) {
        _cellViewModel = [[HWTweetCellViewModel alloc] init];
        _cellViewModel.commentSubject = self.commentSubject;
    }
    return _cellViewModel;
}


- (RACSubject *)commentSubject{
    if (!_commentSubject) {
        _commentSubject = [RACSubject subject];
    }
    return _commentSubject;
}


- (NSMutableArray *)dataArray {
    
    if (!_dataArray) {
        
        _dataArray = [[NSMutableArray alloc] init];
    }
    
    return _dataArray;
}

@end
